
const app = getApp();
const api = app.apiInstance
import { getUserInfo } from '../../utils/store';
Page({
  data: {
    currentStep: 0,
    tabStatus: [{
      label: '未填写',
      value: ''
    }, {
      label: '进行中',
      value: 'process'
    }, {
      label: '已完成',
      value: 'finish'

    }],
    windowWidth: 0,
    id: '',
    formData: {},
    isEdit: true,
    sapId: '',
    readOnlyItem: {},

    userInfo: {
      sapid: '',
      userName: getUserInfo().siocPYName,
      nickName: getUserInfo().siocCnName,
      createTime: app.moment().format('YYYY-MM-DD HH:mm:ss'),
    },
    type: '',
    zongType: '',

  },
  async loadData(id) {
    if (id) {
      const formData = await api.get(`/v1/openApi/resource/change/details/${id}`);
      const current = formData.data.value.find(item => item.auditor == this.data.sapId &&item.remark.indexOf('审核')>0);
      console.log('current', current)
      console.log('isedit', this.data.isEdit)
      const shenHeType = this.checkType(current.remark);
      console.log('shenHeType', shenHeType)

      console.log('current', current);
      this.setData({
        formData: formData.data,
        current: current,
        type: shenHeType,
        remark: current.remark
      })

      if(!this.data.isEdit){
        
        this.setData({
          readOnlyItem: formData.data.key.contentMap[shenHeType],
        })

      }


    }
  },

  onLoad(params) {
    console.log('params', params);
    this.setData({
      id: params.id,
      sapId:params.sapid||getUserInfo().siocSapNo,
      'userInfo.sapid':params.sapid||getUserInfo().siocSapNo,
      isEdit:params.isEdit=='false'?false:true
    })
    this.loadData(params.id);
    var systemInfo = wx.getSystemInfoSync();
    this.setData({
      windowWidth: systemInfo.windowWidth
    })

  },

  checkType(type) {
    if (type.indexOf('消防审核') > -1) {
      return 'xiaoFangShenHe'

    }
    if (type.indexOf('职业健康') > -1) {
      return 'zhiYeJianKangSh'

    }
    if (type.indexOf('环保审核') > -1) {
      return 'huanBaoLeiShenHe'

    }
    if (type.indexOf('安全审核') > -1) {
      return 'anQuanShenHe'

    }
    if (type.indexOf('应急管理审核') > -1) {
      return 'yingJiGlSh'

    }
    if (type.indexOf('应急管理审核') > -1) {
      return 'yingJiGlSh'

    }
    if (type.indexOf('分管副总') > -1) {
      return 'fenGuanFzShenHe'

    }
    if (type.indexOf('副总') > -1) {
      return 'fuZongShenHe'

    }
    if (type.indexOf('总经理') > -1) {
      return 'zongJingLiShenHe'
    }

  },

  async submit() {
    if(!this.data.isEdit){
      wx.navigateBack({
        delta: 1
      });
      return;
    }
    const current = this.data.current;
    console.log('current', current)



    return {
      xiaoFangShenHe: async () => {
        const xiaoFangShenHe = this.selectComponent("#xiaoFangShenHe").data;
        await api.post(`/v1/openApi/resource/change/completelyStep/${this.data.id}/${current.step}/${this.data.sapId}`);

        const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { xiaoFangShenHe: { ...xiaoFangShenHe.form, userInfo: this.data.userInfo, title: '消防审核' } })
        console.log('content', content)
        const updateStatusParams = {
          id: this.data.id,
          sapid: this.data.sapId,
          data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: 'ToBeReviewed' })
        }
        console.log('111', { ...updateStatusParams.data })
        //改变整个主数据
        await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });
        wx.showToast({
          title: '确认成功',
          icon: 'none',
          duration: 2000
        })
        wx.navigateTo({
          url: `/subPages/bianGengSq/bianGengSq`,
        })
        return;
      },

      zhiYeJianKangSh: async () => {
        const zhiYeJianKangSh = this.selectComponent("#zhiYeJianKangSh").data;
        await api.post(`/v1/openApi/resource/change/completelyStep/${this.data.id}/${current.step}/${this.data.sapId}`);

        const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { zhiYeJianKangSh: { ...zhiYeJianKangSh.form, userInfo: this.data.userInfo, title: '职业健康管理审核' } })
        console.log('content', content)
        const updateStatusParams = {
          id: this.data.id,
          sapid: this.data.sapId,
          data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: 'ToBeReviewed' })
        }
        console.log('111', { ...updateStatusParams.data })
        //改变整个主数据
        await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });
        wx.showToast({
          title: '确认成功',
          icon: 'none',
          duration: 2000
        })
        wx.navigateTo({
          url: `/subPages/bianGengSq/bianGengSq`,
        })
        return;
      },

      huanBaoLeiShenHe: async () => {
        const huanBaoLeiShenHe = this.selectComponent("#huanBaoLeiShenHe").data;
        await api.post(`/v1/openApi/resource/change/completelyStep/${this.data.id}/${current.step}/${this.data.sapId}`);

        const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { huanBaoLeiShenHe: { ...huanBaoLeiShenHe.form, userInfo: this.data.userInfo, title: '环保类审核' } })
        console.log('content', content)
        const updateStatusParams = {
          id: this.data.id,
          sapid: this.data.sapId,
          data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: 'ToBeReviewed' })
        }
        console.log('111', { ...updateStatusParams.data })
        //改变整个主数据
        await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });
        wx.showToast({
          title: '确认成功',
          icon: 'none',
          duration: 2000
        })
        wx.navigateTo({
          url: `/subPages/bianGengSq/bianGengSq`,
        })
        return;
      },

      anQuanShenHe: async () => {

        const anQuanShenHe = this.selectComponent("#anQuanShenHe").data;

        if (!anQuanShenHe.form.result) {
          wx.showToast({
            title: '请填写确认状态',
            icon: 'none',
            duration: 2000
          })
          return;
        }
        current.status='Reject'
        if (anQuanShenHe.form.result == '不同意项目实施') {
          await api.post(`/v1/openApi/resource/change/process/${this.data.sapId}`, {
            ...current
          });

          const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { anQuanShenHe: { ...anQuanShenHe.form, userInfo: this.data.userInfo, title: '安全审核' } })
          console.log('content', content)


          const updateStatusParams = {
            id: this.data.id,
            sapid: this.data.sapId,
            data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: 'Reject' })
          }
          console.log('111', { ...updateStatusParams.data })
          //改变整个主数据
          await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });

        }
        else {
          await api.post(`/v1/openApi/resource/change/completelyStep/${this.data.id}/${current.step}/${this.data.sapId}`);

          const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { anQuanShenHe: { ...anQuanShenHe.form, userInfo: this.data.userInfo, title: '安全审核' } })
          console.log('content', content)
          const updateStatusParams = {
            id: this.data.id,
            sapid: this.data.sapId,
            data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: 'ToBeReviewed' })
          }
          console.log('111', { ...updateStatusParams.data })
          //改变整个主数据
          await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });
        }
        wx.showToast({
          title: '确认成功',
          icon: 'none',
          duration: 2000
        })
        wx.navigateTo({
          url: `/subPages/bianGengSq/bianGengSq`,
        })
        return;
      },

      yingJiGlSh: async () => {
        const yingJiGlSh = this.selectComponent("#yingJiGlSh").data;
        if (!yingJiGlSh.form.result) {
          wx.showToast({
            title: '请填写确认状态',
            icon: 'none',
            duration: 2000
          })
          return;
        }
        current.status='Reject'
        if (yingJiGlSh.form.result == '不同意项目实施') {
          await api.post(`/v1/openApi/resource/change/process/${this.data.sapId}`, {
            ...current
          });

          const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { yingJiGlSh: { ...yingJiGlSh.form, userInfo: this.data.userInfo, title: '应急管理审核' } })
          console.log('content', content)


          const updateStatusParams = {
            id: this.data.id,
            sapid: this.data.sapId,
            data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: 'Reject' })
          }
          console.log('111', { ...updateStatusParams.data })
          //改变整个主数据
          await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });

        }
        else {
          await api.post(`/v1/openApi/resource/change/completelyStep/${this.data.id}/${current.step}/${this.data.sapId}`);

          const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { yingJiGlSh: { ...yingJiGlSh.form, userInfo: this.data.userInfo, title: '应急管理审核' } })
          console.log('content', content)
          const updateStatusParams = {
            id: this.data.id,
            sapid: this.data.sapId,
            data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: 'ToBeReviewed' })
          }
          console.log('111', { ...updateStatusParams.data })
          //改变整个主数据
          await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });
        }
        wx.showToast({
          title: '确认成功',
          icon: 'none',
          duration: 2000
        })
        wx.navigateTo({
          url: `/subPages/bianGengSq/bianGengSq`,
        })
        return;
      },

      gaoCengShenHe: async () => {
        const gaoCengShenHe = this.selectComponent("#gaoCengShenHe").data;
        if (!gaoCengShenHe.form.result) {
          wx.showToast({
            title: '请填写确认状态',
            icon: 'none',
            duration: 2000
          })
          return;
        }
        current.status='Reject'
        if (gaoCengShenHe.form.result == '不通过') {
          await api.post(`/v1/openApi/resource/change/process/${this.data.sapId}`, {
            ...current
          });

          const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { [this.data.type]: { ...gaoCengShenHe.form, userInfo: this.data.userInfo, title: this.data.remark } })
          console.log('content', content)


          const updateStatusParams = {
            id: this.data.id,
            sapid: this.data.sapId,
            data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: 'Reject' })
          }
          console.log('111', { ...updateStatusParams.data })
          //改变整个主数据
          await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });

        }
        else {
          await api.post(`/v1/openApi/resource/change/completelyStep/${this.data.id}/${current.step}/${this.data.sapId}`);

          const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { [this.data.type]: { ...gaoCengShenHe.form, userInfo: this.data.userInfo, title: this.data.remark } })
          console.log('content', content)
          const updateStatusParams = {
            id: this.data.id,
            sapid: this.data.sapId,
            data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: this.data.type=='zongJingLi'?'ToBeAcceptance':'ToBeReviewed' })
          }
          console.log('111', { ...updateStatusParams.data })
          //改变整个主数据
          await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });
        }
        wx.showToast({
          title: '确认成功',
          icon: 'none',
          duration: 2000
        })
        wx.navigateTo({
          url: `/subPages/bianGengSq/bianGengSq`,
        })
        return;
      },

      fenGuanFzShenHe:async ()=>{
        const gaoCengShenHe = this.selectComponent("#gaoCengShenHe").data;
        if (!gaoCengShenHe.form.result) {
          wx.showToast({
            title: '请填写确认状态',
            icon: 'none',
            duration: 2000
          })
          return;
        }
        current.status='Reject'
        if (gaoCengShenHe.form.result == '不通过') {
          await api.post(`/v1/openApi/resource/change/process/${this.data.sapId}`, {
            ...current
          });

          const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { [this.data.type]: { ...gaoCengShenHe.form, userInfo: this.data.userInfo, title: this.data.remark } })
          console.log('content', content)


          const updateStatusParams = {
            id: this.data.id,
            sapid: this.data.sapId,
            data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: 'Reject' })
          }
          console.log('111', { ...updateStatusParams.data })
          //改变整个主数据
          await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });

        }
        else {
          await api.post(`/v1/openApi/resource/change/completelyStep/${this.data.id}/${current.step}/${this.data.sapId}`);

          const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { [this.data.type]: { ...gaoCengShenHe.form, userInfo: this.data.userInfo, title: this.data.remark } })
          console.log('this.data.type', this.data.type)
          const updateStatusParams = {
            id: this.data.id,
            sapid: this.data.sapId,
            data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: this.data.type=='zongJingLiShenHe'?'ToBeAcceptance':'ToBeReviewed' })
          }
          console.log('111', { ...updateStatusParams.data })
          //改变整个主数据
          await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });
        }
        wx.showToast({
          title: '确认成功',
          icon: 'none',
          duration: 2000
        })
        wx.navigateTo({
          url: `/subPages/bianGengSq/bianGengSq`,
        })
        return;
      },

      fuZongShenHe:async ()=>{
        const gaoCengShenHe = this.selectComponent("#gaoCengShenHe").data;
        if (!gaoCengShenHe.form.result) {
          wx.showToast({
            title: '请填写确认状态',
            icon: 'none',
            duration: 2000
          })
          return;
        }
        current.status='Reject'
        if (gaoCengShenHe.form.result == '不通过') {
          await api.post(`/v1/openApi/resource/change/process/${this.data.sapId}`, {
            ...current
          });

          const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { [this.data.type]: { ...gaoCengShenHe.form, userInfo: this.data.userInfo, title: this.data.remark } })
          console.log('content', content)


          const updateStatusParams = {
            id: this.data.id,
            sapid: this.data.sapId,
            data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: 'Reject' })
          }
          console.log('111', { ...updateStatusParams.data })
          //改变整个主数据
          await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });

        }
        else {
          await api.post(`/v1/openApi/resource/change/completelyStep/${this.data.id}/${current.step}/${this.data.sapId}`);

          const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { [this.data.type]: { ...gaoCengShenHe.form, userInfo: this.data.userInfo, title: this.data.remark } })
          console.log('content', content)
          const updateStatusParams = {
            id: this.data.id,
            sapid: this.data.sapId,
            data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: this.data.type=='zongJingLiShenHe'?'ToBeAcceptance':'ToBeReviewed' })
          }
          console.log('111', { ...updateStatusParams.data })
          //改变整个主数据
          await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });
        }
        wx.showToast({
          title: '确认成功',
          icon: 'none',
          duration: 2000
        })
        wx.navigateTo({
          url: `/subPages/bianGengSq/bianGengSq`,
        })
        return;
      },

      zongJingLiShenHe:async ()=>{
        const gaoCengShenHe = this.selectComponent("#gaoCengShenHe").data;
        if (!gaoCengShenHe.form.result) {
          wx.showToast({
            title: '请填写确认状态',
            icon: 'none',
            duration: 2000
          })
          return;
        }
        current.status='Reject'
        if (gaoCengShenHe.form.result == '不通过') {
          await api.post(`/v1/openApi/resource/change/process/${this.data.sapId}`, {
            ...current
          });

          const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { [this.data.type]: { ...gaoCengShenHe.form, userInfo: this.data.userInfo, title: this.data.remark } })
          console.log('content', content)


          const updateStatusParams = {
            id: this.data.id,
            sapid: this.data.sapId,
            data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: 'Reject' })
          }
          console.log('111', { ...updateStatusParams.data })
          //改变整个主数据
          await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });

        }
        else {
          await api.post(`/v1/openApi/resource/change/completelyStep/${this.data.id}/${current.step}/${this.data.sapId}`);

          const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { [this.data.type]: { ...gaoCengShenHe.form, userInfo: this.data.userInfo, title: this.data.remark } })
          console.log('content', content)
          const updateStatusParams = {
            id: this.data.id,
            sapid: this.data.sapId,
            data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: this.data.type=='zongJingLiShenHe'?'ToBeAcceptance':'ToBeReviewed' })
          }
          console.log('111', { ...updateStatusParams.data })
          //改变整个主数据
          await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });
        }
        wx.showToast({
          title: '确认成功',
          icon: 'none',
          duration: 2000
        })
        wx.navigateTo({
          url: `/subPages/bianGengSq/bianGengSq`,
        })
        return;
      }


    }[this.data.type]()


    // const shenHe = this.selectComponent(`#${this.data.type}`).data;
    // await api.post(`/v1/openApi/resource/change/completelyStep/${this.data.id}/${current.step}/${this.data.sapId}`);

    // const content = Object.assign({}, { ...this.data.formData.key.contentMap }, { [this.data.type]: { ...shenHe.form, userInfo: this.data.userInfo, title: this.data.remark } })
    // console.log('content', content)
    // const updateStatusParams = {
    //   id: this.data.id,
    //   sapid: this.data.sapId,
    //   data: Object.assign({}, { ...this.data.formData.key }, { content: JSON.stringify(content) }, { status: 'ToBeReviewed' })
    // }
    // console.log('111', { ...updateStatusParams.data })
    // //改变整个主数据
    // await api.post(`/v1/openApi/resource/change/updateStatus/${this.data.id}/${this.data.sapId}`, { ...updateStatusParams.data });
    // wx.showToast({
    //   title: '确认成功',
    //   icon: 'none',
    //   duration: 2000
    // })
    // wx.navigateTo({
    //   url: `/subPages/bianGengSq/bianGengSq`,
    // })
    // return;


  }

})
